﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace WindowsGame1
{
    class ObjectManager
    {
        public List<BaseObject> m_ObjectList = new List<BaseObject>();
        public static ObjectManager m_ObjectManager = new ObjectManager();

        public ObjectManager()
        {

        }

        public void Update(float deltaT, KeyboardState LastKeyState)
        {
            foreach (BaseObject obj in m_ObjectList)
            {
                obj.Update(deltaT, LastKeyState);
                foreach (BaseObject other in m_ObjectList)
                {
                    if (other != obj)
                    {
                        Rectangle Rect1 = new Rectangle((int)obj.m_Position.X - 32, (int)obj.m_Position.Y - 128, 64, 128);
                        Rectangle Rect2 = new Rectangle((int)other.m_Position.X - 64, (int)other.m_Position.Y - 128, 128, 128);
                        if (Rect1.Intersects(Rect2))
                        {
                            Rectangle OutRect;
                            Rectangle.Intersect(ref Rect1, ref Rect2, out OutRect);
                            obj.CollisionResponse(other, OutRect);
                        }
                    }
                }
            }
        }

        public void Render(SpriteBatch spriteBatch, Camera cam)
        {
            foreach (BaseObject obj in m_ObjectList)
            {
                obj.Render(spriteBatch, cam);
            }
        }

        public Player GetPlayer() {

            foreach (BaseObject obj in m_ObjectList)
            {
                if (obj.m_ObjectType == BaseObject.OBJ_TYPE.OBJ_PLAYER)
                {
                    return (Player)obj;
                }
            }

            return null;
        }

    }
}
